package com.zhangpp.serviceImpl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangpp.dao.FeedbackMapper;
import com.zhangpp.entity.Feedback;
import com.zhangpp.service.FeedbackService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Resource
    FeedbackMapper feedbackMapper;

    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);

        int count = feedbackMapper.selectCount(null);

        map.put("total",count);
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        IPage<Feedback> page1 = new Page<>(page,pageSize);
        IPage<Feedback> feedbackIPage = feedbackMapper.selectPage(page1, null);

        //设置数据
        map.put("rows",feedbackIPage.getRecords());

        return map;
    }

    @Override
    public HashMap<String, Object> delete(Feedback feedback) {

        HashMap<String, Object> map = new HashMap<>();
        try {
            QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
            QueryWrapper<Feedback> id = wrapper.eq("id", feedback.getId());
            feedbackMapper.delete(id);
            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }
        return map;
    }

}
